/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gblend.exactquery;

import gblend.adjlistgraph.Link;
import gblend.adjlistgraph.Node;
import java.util.Stack;

/**
 *
 * @author c4jin
 */
public class SpindleGraph {

    private SNode[] vertexs;
    private Link[] adjTab = null;
    private int pos = -1;
    private int gid;

    public SpindleGraph(int size) {
        vertexs = new SNode[size];
        pos = -1;
        adjTab = new Link[size];
        for (int i = 0; i < size; i++) {
            adjTab[i] = new Link();
        }
    }

    public void addnode(SNode node) {
        vertexs[++pos] = node;
    }

    public int getSize() {
        return pos + 1;
    }

    public int getId() {
        return gid;
    }

    public void setId(int id) {
        gid = id;
    }

    public SNode getNode(int index) {
        return vertexs[index];
    }

    public int getEdge(int from, int to) {
        Node p = adjTab[from].first();
        while (p != null) {
            if (p.index() == to) {
                return 1;
            }
            p = p.next();
        }

        return 0;
    }

    public void addEdge(int from, int to) {
        adjTab[from].add(to);
    }

    public int findNeighbor(int index) {
        for (int i = 0; i <= pos; i++) {  //here is a reverse search
            if (getEdge(i, index) == 1 && !vertexs[i].isVisited()) {
                return i;
            }
        }
        return -1;
    }

    public void dsf(int index) {
        int inital = index;
        if (vertexs[index] == null) {
            return;
        }

        Stack s = new Stack();
        vertexs[index].visit();
        s.push(index);

        while (!s.isEmpty()) {
            index = findNeighbor((Integer) s.peek());
            if (index != -1) {
                vertexs[index].visit();
                s.push(index);
                vertexs[inital].setSucc(index);
            } else {
                s.pop();
            }
        }
        clean();
    }

    public void clean() {
        for (SNode v : vertexs) {
            if (v != null) {
                v.clean();
            }
        }
    }

    public void print() {
        System.out.println("*** GID=" + gid + " *** size = " + vertexs.length);
        for (int i = 0; i < vertexs.length; i++) {
            if (vertexs[i] != null) {
                System.out.print("i="+i);
                vertexs[i].print();
            }
        }
        System.out.println("-Connectivity:");
        for (int i = 0; i < adjTab.length; i++) {
            Node nd = adjTab[i].next();
            if (nd != null) {
                System.out.print("From " + i + " to:");
                System.out.print(" " + nd.index());
                while (adjTab[i].hasNext()) {
                    System.out.print(" " + adjTab[i].next().index());
                }
                System.out.println();
            }
            
        }
        System.out.println(" *******");
    }
}
